﻿using MovieBrowser.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using NymphsLib.CommonUtilities.Extensions;

namespace MovieBrowser.Util
{
    public class ImdbParserHelper
    {
        public static Movie ToMovie(Dictionary<string, object> parsed)
        {
            try
            {
                var movie = new Movie();

                movie.ImdbId = ((string)parsed[Parser.ImdbParser.KEY_ImdbId]).LimitWithElipses(255);
                movie.Title = ((string)parsed[Parser.ImdbParser.KEY_Title]).LimitWithElipses(255);
                movie.Rating = (double)parsed[Parser.ImdbParser.KEY_Rating];
                movie.Year = (int)parsed[Parser.ImdbParser.KEY_Year];
                //
                movie.Runtime = ("" + (string)parsed[Parser.ImdbParser.KEY_Runtime]).LimitWithElipses(255);
                movie.MPAA = ("" + (string)parsed[Parser.ImdbParser.KEY_Mpaa]).LimitWithElipses(255);
                movie.MPAAReason = ("" + (string)parsed[Parser.ImdbParser.KEY_MpaaReason]).LimitWithElipses(255);
                movie.Highlight = ("" + (string)parsed[Parser.ImdbParser.KEY_Highlight]).LimitWithElipses(255);

                try { movie.ImdbVoters = Int32.Parse((string)parsed[Parser.ImdbParser.KEY_NumVoters]); }
                catch { movie.ImdbVoters = 0; }
                try { movie.ImdbCritics = Int32.Parse((string)parsed[Parser.ImdbParser.KEY_NumCritic]); }
                catch { movie.ImdbCritics = 0; }
                try { movie.MetaScore = Int32.Parse((string)parsed[Parser.ImdbParser.KEY_MetaScore]); }
                catch { movie.MetaScore = 0; }
                try
                {
                    movie.ReleaseDate = Convert.ToDateTime((string)parsed[Parser.ImdbParser.KEY_ReleaseDate]);

                    if (movie.ReleaseDate != null && movie.ReleaseDate.Value.Year < 1900)
                        movie.ReleaseDate = null;
                }
                catch (Exception exp) { Console.Write(exp.Message); }
                movie.Storyline = (string)parsed[Parser.ImdbParser.KEY_Storyline];
                //
                List<Dictionary<string, object>> list;
                {
                    list = (List<Dictionary<string, object>>)parsed[Parser.ImdbParser.KEY_Genres];
                    var items = new List<Genre>();
                    foreach (var dict in list)
                    {
                        items.Add(new Genre()
                        {
                            Code = (string)dict["Code"],
                            Name = (string)dict["Name"]
                        });
                    }
                    movie.Genres = items;
                }
                {
                    list = (List<Dictionary<string, object>>)parsed[Parser.ImdbParser.KEY_Countries];
                    var items = new List<Country>();
                    foreach (var dict in list)
                    {
                        items.Add(new Country()
                        {
                            Code = (string)dict["Code"],
                            Name = (string)dict["Name"]
                        });
                    }
                    movie.Countries = items;
                }
                {
                    list = (List<Dictionary<string, object>>)parsed[Parser.ImdbParser.KEY_Languages];
                    var items = new List<Language>();
                    foreach (var dict in list)
                    {
                        items.Add(new Language()
                        {
                            Code = (string)dict["Code"],
                            Name = (string)dict["Name"]
                        });
                    }
                    movie.Languages = items;
                }
                {
                    list = (List<Dictionary<string, object>>)parsed[Parser.ImdbParser.KEY_Stars];
                    var items = new List<Person>();
                    foreach (var dict in list)
                    {
                        items.Add(new Person()
                        {
                            ImdbId = (string)dict["ImdbId"],
                            Name = (string)dict["Name"]
                        });
                    }
                    movie.PersonStars = items;
                }
                {
                    list = (List<Dictionary<string, object>>)parsed[Parser.ImdbParser.KEY_Writers];
                    var items = new List<Person>();
                    foreach (var dict in list)
                    {
                        items.Add(new Person()
                        {
                            ImdbId = (string)dict["ImdbId"],
                            Name = (string)dict["Name"]
                        });
                    }
                    movie.PersonWriters = items;
                }
                {
                    list = (List<Dictionary<string, object>>)parsed[Parser.ImdbParser.KEY_Directors];
                    var items = new List<Person>();
                    foreach (var dict in list)
                    {
                        items.Add(new Person()
                        {
                            ImdbId = (string)dict["ImdbId"],
                            Name = (string)dict["Name"]
                        });
                    }
                    movie.PersonDirectors = items;
                }

                {
                    list = (List<Dictionary<string, object>>)parsed[Parser.ImdbParser.KEY_Characters];
                    var items = new List<Actor>();
                    foreach (var dict in list)
                    {
                        var persondict = (Dictionary<string, object>)dict["Person"];
                        items.Add(new Actor()
                        {
                            Person = new Person
                            {
                                ImdbId = (string)persondict["ImdbId"],
                                Name = (string)persondict["Name"]
                            },
                            CharacterName = (string)dict["CharacterName"],
                            CharacterId = (string)dict["CharacterId"]
                        });
                    }
                    movie.PersonActors = items;
                }
                return movie;
            }
            catch (Exception exp)
            {
                return null;
            }

        }

        public static List<Keyword> ToKeywords(List<Dictionary<string, object>> keywords)
        {
            var list = new List<Keyword>();
            foreach (var item in keywords)
            {
                list.Add(new Keyword
                {
                    Code = (string)item["Code"],
                    Name = (string)item["Name"]
                });
            }
            return list;
        }

        public static Person ToPerson(Dictionary<string, object> dictionary)
        {
            try
            {
                var p = new Person
                {
                    ImdbId = (string)dictionary[Parser.ImdbParser.KEY_PersonId],
                    Name = (string)dictionary[Parser.ImdbParser.KEY_PersonName],
                    Birthday = (DateTime?)dictionary[Parser.ImdbParser.KEY_PersonBirthday],
                    Deathday = (DateTime?)dictionary[Parser.ImdbParser.KEY_PersonDeathday],
                    Height = (string)dictionary[Parser.ImdbParser.KEY_PersonHeight],
                    Highlight = (string)dictionary[Parser.ImdbParser.KEY_Highlight],
                    Profession = (string)dictionary[Parser.ImdbParser.KEY_PersonProfession],
                    Birthplace = (string)dictionary[Parser.ImdbParser.KEY_PersonBirthplace]
                };

                if (p.Birthplace != null)
                {
                    p.Name = (string)dictionary[Parser.ImdbParser.KEY_PersonCountry];
                    var lastOrDefault = p.Birthplace.Split(",".ToCharArray()).LastOrDefault();
                    if (lastOrDefault != null)
                        p.Country = lastOrDefault.Trim();
                }
                return p;
            }
            catch
            {
                return null;
            }
        }
    }
}
